<template>
  <el-dialog
    v-model="dialogFormVisible"
    :title="title"
    width="700px"
    @close="close"
  >
    <el-form ref="formRef" label-width="120px" :model="form" :rules="rules">
      <!-- <el-form-item label="客户简称" prop="CustomerName">
        <el-input v-model="form.CustomerName" />
      </el-form-item> -->
      <el-form-item label="客户全称" prop="CustomerAllName">
        <el-input v-model="form.CustomerAllName" @input="doesExist" />
      </el-form-item>
      <el-form-item label="所属区域" prop="City">
        <el-cascader
          v-model="form.City"
          :options="cityOptions"
          :props="CityProps"
          placeholder="选择地区"
          style="width: 300px"
        />
      </el-form-item>
      <el-form-item label="所属行业" prop="Industry">
        <el-cascader
          v-model="form.Industry"
          :options="industryOptions"
          :props="IndustryProps"
          placeholder="选择行业"
          filterable
          style="width: 200px"
        />
      </el-form-item>
      <el-form-item label="地址" prop="Address">
        <el-input v-model="form.Address" />
      </el-form-item>
      <el-form-item label="电话" prop="Telphone">
        <el-input v-model="form.Telphone" />
      </el-form-item>
      <el-form-item label="法人代表" prop="LegalPerson">
        <el-input v-model="form.LegalPerson" />
      </el-form-item>
      <el-form-item label="负责人电话" prop="LegalMobile">
        <el-input v-model="form.LegalMobile" />
      </el-form-item>
      <el-form-item label="业务联系人" prop="BusinessPerson">
        <el-input v-model="form.BusinessPerson" />
      </el-form-item>
      <el-form-item label="业务联系人电话" prop="BusinessPersonMobile">
        <el-input v-model="form.BusinessPersonMobile" />
      </el-form-item>
      <el-form-item label="主营产品" prop="SalesProducts">
        <el-input v-model="form.SalesProducts" />
      </el-form-item>
      <el-form-item label="主要销售区域" prop="SalesArea">
        <el-input v-model="form.SalesArea" />
      </el-form-item>
      <el-form-item label="主要客户群体" prop="CustomerGroup">
        <el-input v-model="form.CustomerGroup" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">保 存</el-button>
    </template>
  </el-dialog>
</template>

<script>
  import { AddCustomer, SearchCompanyName } from '@/api/customer'
  import { CitySelect, IndustrySelect } from '@/api/common'
  import { ElMessage } from 'element-plus'

  export default defineComponent({
    name: 'CustomerSearchpickEdit',
    emits: ['fetch-data'],
    setup(props, { emit }) {
      const $baseMessage = inject('$baseMessage')

      const state = reactive({
        formRef: null,
        CityProps: {
          expandTrigger: 'hover',
        },
        IndustryProps: {
          expandTrigger: 'hover',
        },
        form: {
          // roles: [],
          // CustomerName: '',
          CustomerAllName: '',
          ProvinceCode: '',
          City: [],
          Industry: [],
          LegalPerson: '',
          LegalMobile: '',
          Address: '',
          Telphone: '',
          BusinessPerson: '',
          BusinessPersonMobile: '',
          SalesProducts: '',
          SalesArea: '',
          CustomerGroup: '',
        },
        rules: {
          // CustomerName: [
          //   { required: true, trigger: 'change', message: '请输入客户简称' },
          // ],
          CustomerAllName: [
            { required: true, trigger: 'blur', message: '请输入客户全称' },
          ],
          City: [
            { required: true, trigger: 'blur', message: '请选择所在地区' },
          ],
          Industry: [
            { required: true, trigger: 'blur', message: '请选择所属行业' },
          ],
          // LegalPerson: [
          //   { required: true, trigger: 'blur', message: '请输入负责人姓名' },
          // ],
          // LegalMobile: [
          //   {
          //     required: true,
          //     trigger: 'blur',
          //     message: '请输入负责人联系方式',
          //   },
          // ],
          // Address: [{ required: true, trigger: 'blur', message: '请输入地址' }],
          // Telphone: [
          //   { required: true, trigger: 'blur', message: '请输入电话' },
          // ],
          // BusinessPerson: [
          //   { required: true, trigger: 'blur', message: '请输入业务联系人' },
          // ],
          // BusinessPersonMobile: [
          //   {
          //     required: true,
          //     trigger: 'blur',
          //     message: '请输入业务联系人手机号',
          //   },
          // ],
          // SalesProducts: [
          //   { required: true, trigger: 'blur', message: '请输入主营产品' },
          // ],
          // SalesArea: [
          //   { required: true, trigger: 'blur', message: '请输入销售区域' },
          // ],
          // CustomerGroup: [
          //   { required: true, trigger: 'blur', message: '请输入主要客户群体' },
          // ],
        },
        title: '',
        dialogFormVisible: false,
        cityOptions: [],
        industryOptions: [],
        citySelect: { checkStrictly: true },
        industrySelect: { checkStrictly: true },
      })

      const showEdit = (row) => {
        state.title = '添加'
        state.form = Object.assign({}, row)
        state.dialogFormVisible = true
      }
      const close = () => {
        state['formRef'].resetFields()
        state.form = {
          // roles: [],
        }
        state.dialogFormVisible = false
      }
      const doesExist = async (val) => {
        state.form.CustomerAllName = val.replace(
          /[^\u4e00-\u9fa5a-zA-Z0-9]/g,
          ''
        )
        const dataMsg = {
          CustomerAllName: state.form.CustomerAllName,
        }
        const { data, msg } = await SearchCompanyName(dataMsg)
        if (data == 1) {
          ElMessage({
            showClose: true,
            duration: 4000,
            message: msg,
            type: 'error',
          })
        }

        console.log(data, 'datadata')
        console.log(val, '123123')
      }

      const save = () => {
        state['formRef'].validate(async (valid) => {
          if (valid) {
            const { msg } = await AddCustomer(state.form)
            $baseMessage(msg, 'success', 'vab-hey-message-success')
            emit('fetch-data', state.form.CustomerAllName)
            close()
          }
        })
      }

      const fetchData = async () => {
        state.listLoading = true
        const {
          data: { cityOptions },
        } = await CitySelect()
        const {
          data: { industryOptions },
        } = await IndustrySelect()
        state.cityOptions = cityOptions
        state.industryOptions = industryOptions
        state.listLoading = false
      }
      onMounted(() => {
        fetchData()
      })

      return {
        ...toRefs(state),
        showEdit,
        doesExist,
        close,
        save,
      }
    },
  })
</script>
